package com.fescoservice.dao;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.fescoservice.exception.FSException;
import com.fescoservice.model.BBSAttach;
import com.fescoservice.model.BBSAttachId;
import com.fescoservice.model.BBSId;

/**
 * A data access object (DAO) providing persistence and search support for
 * BBSAttach entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.fescoservice.dao.BBSAttach
 * @author MyEclipse Persistence Tools
 */

public class BBSAttachDAO extends HibernateDaoSupport {
	private static final Log log = LogFactory.getLog(BBSAttachDAO.class);
	// property constants
	public static final String ATTACH_PATH = "attachPath";
	public static final String ATTACH_NAME = "attachName";
	public static final String ATTACH_SIZE = "attachSize";
	public static final String REAL_NAME = "realName";
	public static final String ATTACH_TYPE = "attachType";

	protected void initDao() {
		// do nothing
	}

	public void save(BBSAttach transientInstance) {
		log.debug("saving BBSAttach instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(BBSAttach persistentInstance) {
		log.debug("deleting BBSAttach instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}
	
	public Long countBBSAttachId(final String sql ,final BBSId id) throws FSException{
		log.debug("finding Category instance by example");
		try {
			Long count = (Long) getHibernateTemplate().execute(
					   new HibernateCallback() {
					   public Object doInHibernate(Session session)
					   throws HibernateException, SQLException {
						   SQLQuery q = session.createSQLQuery(sql);
						   q.setString("boardCode1" , id.getBoardCode1());   
						   q.setString("boardCode2" , id.getBoardCode2());   
						   q.setString("boardCode3" , id.getBoardCode3()); 
						   q.setLong("boardSeq" , id.getBoardSeq()); 
						  return q.addScalar( "cnt" , Hibernate.LONG).uniqueResult();
					   }
					 } );
			return count;
		} catch (Exception re) {
			log.error("count categories failed", re);
			throw new FSException("FS_ERROR_COUNTBBS",re.getMessage());
		}
	}
	
	public List findBBSAttachesById(final String hql,final BBSId bbsId)  throws FSException{
		try {
			List result = (List) getHibernateTemplate().execute(
					   new HibernateCallback() {
					   public Object doInHibernate(Session session)
					   throws HibernateException, SQLException {
						   Query q = session.createQuery(hql);
						   q.setString("boardCode1" , bbsId.getBoardCode1());   
						   q.setString("boardCode2" , bbsId.getBoardCode2());   
						   q.setString("boardCode3" , bbsId.getBoardCode3());  
						   q.setLong("boardSeq" , bbsId.getBoardSeq());  
						  return q.list();
					   }
					 } );
			return result;
		} catch (RuntimeException re) {
			log.error("get code1names failed", re);
			throw new FSException("FS_ERROR_GETCODE2NAMES",re.getMessage());
		}
	}

	public BBSAttach findById(BBSAttachId id) {
		log.debug("getting BBSAttach instance with id: " + id);
		try {
			BBSAttach instance = (BBSAttach) getHibernateTemplate().get(
					"com.fescoservice.model.BBSAttach", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(BBSAttach instance) {
		log.debug("finding BBSAttach instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding BBSAttach instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from BBSAttach as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByAttachPath(Object attachPath) {
		return findByProperty(ATTACH_PATH, attachPath);
	}

	public List findByAttachName(Object attachName) {
		return findByProperty(ATTACH_NAME, attachName);
	}

	public List findByAttachSize(Object attachSize) {
		return findByProperty(ATTACH_SIZE, attachSize);
	}

	public List findByRealName(Object realName) {
		return findByProperty(REAL_NAME, realName);
	}

	public List findByAttachType(Object attachType) {
		return findByProperty(ATTACH_TYPE, attachType);
	}

	public List findAll() {
		log.debug("finding all BBSAttach instances");
		try {
			String queryString = "from BBSAttach";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public BBSAttach merge(BBSAttach detachedInstance) {
		log.debug("merging BBSAttach instance");
		try {
			BBSAttach result = (BBSAttach) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(BBSAttach instance) {
		log.debug("attaching dirty BBSAttach instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(BBSAttach instance) {
		log.debug("attaching clean BBSAttach instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static BBSAttachDAO getFromApplicationContext(ApplicationContext ctx) {
		return (BBSAttachDAO) ctx.getBean("BBSAttachDAO");
	}
}